using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;
namespace WindowsGame4
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        RenderManager rMan;
        public static List<GameObject> objects, newObjects;
        public static Texture2D texture;
        public static int width=1440, height=900;
        public static Random random;
        //public static GameObject obj, cursor, ray;
        public static Player player;
        public static float dt;
        public static SpriteFont basicFont;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = height;
            graphics.PreferredBackBufferWidth = width;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            objects = new List<GameObject>();
            newObjects = new List<GameObject>();
            rMan = new RenderManager(graphics, objects, width, height);
            random = new Random();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            texture = Content.Load<Texture2D>("GameThumbnail");
            basicFont = Content.Load<SpriteFont>("BasicFont");
            ParticleEmitter emitter = new ParticleEmitter();
            emitter.texture = texture;
            emitter.color = new Vector4(1, 1, 0, 1);
            emitter.target = emitter;
            emitter.minScale = .25f;
            emitter.maxDistance = 100;
            emitter.minRotSpeed = -50;
            emitter.maxRotSpeed = 50;
            emitter.moveSpeed = -300;
            emitter.pos = new Vector2(300, 300);
            emitter.nEmits = 3;
            emitter.scaleRate = 1;
            emitter.SetEmissionRate(200);
            emitter.maxAngle = 270;
            emitter.bCollides = true;
            //objects.Add(emitter);
            //player
            player = new Player();
            player.pos = new Vector2(720, 450);
            player.texture = texture;
            player.moveAcc = 300;
            player.depth = 0;
            player.bMoves = false;
            objects.Add(player);
            //emitter
            emitter = new AsteroidEmitter();
            emitter.attachTarget = player;
            emitter.nEmits = 1;
            emitter.maxEmits = 50;
            emitter.maxRotSpeed = 50;
            emitter.minRotSpeed = -50;
            emitter.minDistance = 500;
            emitter.maxDistance = 700;
            emitter.fadeRate = 0;
            emitter.moveSpeed = 50;
            emitter.particleLifeTime = 1000;
            objects.Add(emitter);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public static void AddObject(GameObject obj)
        {
            newObjects.Add(obj);
        }
        protected override void Update(GameTime gameTime)
        {
            List<GameObject> deadObjects = new List<GameObject>();
            dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Physics.Collide(objects);
            foreach (GameObject obj in objects)
            {
                if (obj.bDead)
                    deadObjects.Add(obj);
                else obj.Update(dt);
            }
            foreach (GameObject obj in deadObjects)
            {
                obj.OnDeath();
                objects.Remove(obj);
            }
            foreach (GameObject obj in newObjects)
                objects.Add(obj);
            newObjects.Clear();
            Input(dt);
            base.Update(gameTime);
        }
        void Input(float time)
        {
            player.Input();
            KeyboardState keyboard = Keyboard.GetState();
            if (keyboard.IsKeyDown(Keys.Escape))
                Exit();
        }
        public static Vector2 GetCenter()
        {
            Vector2 center = new Vector2(width / 2, height / 2);
            return center;
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            rMan.Draw();
            base.Draw(gameTime);
        }
    }
}
